﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace application
{
    public partial class baseFormV2 : commonV2.forms.baseForm
    {
        public baseFormV2()
        {
            InitializeComponent();
            SetMenuVisible(false);
        }
        protected void SetMenuVisible(bool v)
        {
            optionMenuItem.Visible = v;
            mainMenuStrip.Visible = v;
        }
        protected void SetMenuEnable(bool v)
        {
            optionMenuItem.Enabled = v;
        }

        protected void MenuEnableSetSTATUS(bool v)
        {
            setStatusMenuItem.Enabled = v;
        }
        protected void MenuVisibleSetSTATUS(bool v)
        {
            setStatusMenuItem.Visible = v;
        }

        protected override void ErrorHandler(object sender, Exception er)
        {
            base.ErrorHandler(sender, er);
            try
            {
                if (CommonTypes.Settings.sysLogException)
                    DataAccess.DataLibs.AddSysLogERROR(er);
            }
            catch
            {
                CommonTypes.Settings.sysLogException = false;
            }
        }

        public string myFormCode = "";
        protected override commonV2.myTypes.Permissions GetFormPermission() 
        {
            commonV2.myTypes.Permissions permV2 = new commonV2.myTypes.Permissions();
            if (this.myFormCode == null || this.myFormCode.Trim() == "") return permV2;
            common.myTypes.Permissions perms = application.SysLibs.GetFormPermission(this.myFormCode);
            MenuEnableSetSTATUS(perms.Edit);
            permV2.Add = perms.Add;
            permV2.Edit = perms.Edit;
            permV2.Delete = perms.Delete;
            return permV2;
        }

        private application.forms.statusBase _mySetStatusOptions = null;
        protected application.forms.statusBase mySetStatusOptionForm
        {
            get
            {
                if (_mySetStatusOptions == null || _mySetStatusOptions.IsDisposed)
                {
                    _mySetStatusOptions = CreateStatusOptionForm();
                    if (_mySetStatusOptions != null)
                        _mySetStatusOptions.myOnProcess += new common.forms.baseDialogForm.onProcess(SetStatusHandler);
                }
                return _mySetStatusOptions;
            }
        }


        protected virtual bool SetStatusValidate() { return true; }
        protected virtual forms.statusBase CreateStatusOptionForm() { return null; }
        protected virtual void SetStatus(common.CommonStatus value) { }
        protected void DoExportData(DataTable dataTbl)
        {
            common.DialogLibs.SetFileDialogEXCEL(this.saveFileDialog);
            if (this.saveFileDialog.ShowDialog() != DialogResult.OK) return;
            bool retVal = commonV2.ExportLibs.ExportToExcel(dataTbl, this.saveFileDialog.FileName);
            if (retVal) common.SysLibs.ShowMessage(String.Format(common.language.GetString("dataSavedTo"), this.saveFileDialog.FileName));
            else common.SysLibs.ShowMessage(common.language.GetString("errorOccured"));
        }

        private void SetStatusHandler(object sender, common.myTypes.DialogEvent e)
        {
            if (mySetStatusOptionForm == null) return;
            if (e.isCloseClick) return;
            SetStatus(mySetStatusOptionForm.myStatus);
            e.acceptClose = true;
        }

        private void setStatusMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (mySetStatusOptionForm == null) return;
                if (SetStatusValidate() == false) return;
                mySetStatusOptionForm.ShowForm();
            }
            catch (Exception er)
            {
                this.ErrorHandler(this,er);
            }
        }
        private void printMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Print(printMenuItem.Checked == false);
            }
            catch (Exception er)
            {
                this.ErrorHandler(this, er);
            }
        }
        private void exportMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Export();
            }
            catch (Exception er)
            {
                this.ErrorHandler(this, er);
            }
        }
    }
}
